package com.ruoyi.medication.service.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.medication.domain.Medication;
import com.ruoyi.medication.domain.MedicationStock;
import com.ruoyi.medication.mapper.MedicationMapper;
import com.ruoyi.medication.service.IMedicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 药物Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-12
 */
@Service
public class MedicationServiceImpl implements IMedicationService 
{
    @Autowired
    private MedicationMapper medicationMapper;

    /**
     * 查询药物
     * 
     * @param id 药物主键
     * @return 药物
     */
    @Override
    public Medication selectMedicationById(Long id)
    {
        return medicationMapper.selectMedicationById(id);
    }

    /**
     * 查询药物列表
     * 
     * @param medication 药物
     * @return 药物
     */
    @Override
    public List<Medication> selectMedicationList(Medication medication)
    {
        return medicationMapper.selectMedicationList(medication);
    }

    /**
     * 新增药物
     * 
     * @param medication 药物
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMedication(Medication medication)
    {
        int rows = medicationMapper.insertMedication(medication);
        insertMedicationStock(medication);
        return rows;
    }

    /**
     * 修改药物
     * 
     * @param medication 药物
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMedication(Medication medication)
    {
        medicationMapper.deleteMedicationStockByMedicationId(medication.getId());
        insertMedicationStock(medication);
        return medicationMapper.updateMedication(medication);
    }

    /**
     * 批量删除药物
     * 
     * @param ids 需要删除的药物主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMedicationByIds(Long[] ids)
    {
        medicationMapper.deleteMedicationStockByMedicationIds(ids);
        return medicationMapper.deleteMedicationByIds(ids);
    }

    /**
     * 删除药物信息
     * 
     * @param id 药物主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMedicationById(Long id)
    {
        medicationMapper.deleteMedicationStockByMedicationId(id);
        return medicationMapper.deleteMedicationById(id);
    }

    /**
     * 新增药物库存信息
     * 
     * @param medication 药物对象
     */
    public void insertMedicationStock(Medication medication)
    {
        List<MedicationStock> medicationStockList = medication.getMedicationStockList();
        Long id = medication.getId();
        if (StringUtils.isNotNull(medicationStockList))
        {
            List<MedicationStock> list = new ArrayList<MedicationStock>();
            for (MedicationStock medicationStock : medicationStockList)
            {
                medicationStock.setMedicationId(id);
                list.add(medicationStock);
            }
            if (list.size() > 0)
            {
                medicationMapper.batchMedicationStock(list);
            }
        }
    }
}
